package com.yc.simplefactory1;

import com.yc.bean.ICourse;
import com.yc.bean.JavaCourse;
import com.yc.bean.PythonCourse;

public class CourseFactory {

    //OPEN-CLOSE PRINCIPL开闭原则,对扩展开放,对修改关闭
    //方案1
    public ICourse create(String name) {
        if("name".equals("java")) {
        return new JavaCourse();
        }else if("name".equals("python")){
            return new PythonCourse();
        }else{
        return null;
        }
    }

    //方案二
    public ICourse create2(String className)  {
        //利用反射完成对象创建
        try{
        if(!(null==className||"".equals(className))) {
            return (ICourse) Class.forName(className).newInstance();
            }
           } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //方案三
    public static ICourse create3(Class<? extends ICourse> clazz) {
        try{
            if(null!=clazz){
                return clazz.newInstance();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
